//
//  YJStatusOriginalView.swift
//  YJWeiBo
//
//  Created by Jim1024 on 2017/1/17.
//  Copyright © 2017年 Jim1024. All rights reserved.
//

import UIKit
import SnapKit

class YJStatusOriginalView: UIView {
    
    /// 底部约束
    var originalViewBottomCons: Constraint?
    
    var status: YJStatus? {
        didSet {
            if let imageURL = status?.user?.profile_image_url {
                let url = URL(string: imageURL)
                avatarView.kf.setImage(with: url, placeholder: defaultAvatar())
            }
            
            nameLabel.text = status?.user?.screen_name
            
            let sinaDateString = status?.created_at ?? "未知时间"
            timeLabel.text = Date.dateWithSinaSting(sinaDateString).sinaDateDescription()
            
            sourceLabel.text = status?.source
            
            memberView.image = status?.user?.mbrankImage
            
            verifiedView.image = status?.user?.verifiedTypeImage
            
            contentLabel.text = status?.text
            
            // 在添加约束之前删除这个约束
            originalViewBottomCons?.deactivate()
            if let pictureURLs = status?.storePictureURLs {
                pictureView.isHidden = false
                pictureView.pictureURLs = pictureURLs
                // 有图片需要显示YJStatusOriginalView的底部就参照picutreView的底部
                self.snp.makeConstraints({ (make) in
                    self.originalViewBottomCons = make.bottom.equalTo(pictureView).offset(YJStatusCellMargin).constraint
                })
            } else {
                pictureView.isHidden = true
                // 没有图片需要显示YJStatusOriginalView的底部就参照conetntLabel的底部
                self.snp.makeConstraints({ (make) in
                    self.originalViewBottomCons = make.bottom.equalTo(contentLabel).offset(YJStatusCellMargin).constraint
                })
                setNeedsLayout()
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.white
        initCustomView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initCustomView() {
        addSubview(avatarView)
        addSubview(verifiedView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        /*================= layout =================*/
        
        // 头像
        avatarView.snp.makeConstraints { (make) in
            make.top.leading.equalToSuperview().offset(YJStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        // 认证图标
        verifiedView.snp.makeConstraints { (make) in
            make.centerX.equalTo(avatarView.snp.trailing)
            make.centerY.equalTo(avatarView.snp.bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        // 用户名称
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(avatarView)
            make.leading.equalTo(avatarView.snp.trailing).offset(YJStatusCellMargin)
        }
        
        // 会员等级
        memberView.snp.makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp.trailing).offset(YJStatusCellMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        // 时间
        timeLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(avatarView)
        }
        
        // 来源
        sourceLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp.trailing).offset(YJStatusCellMargin)
        }
        
        // 文本内容
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(avatarView.snp.bottom).offset(YJStatusCellMargin)
            make.leading.equalTo(avatarView)
            make.trailing.equalToSuperview().offset(-YJStatusCellMargin)
        }
        
        // 配图视图
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(YJStatusCellMargin)
            make.leading.equalToSuperview().offset(YJStatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        // 默认原创cell的底部参照pictureView的底部
        self.snp.makeConstraints { (make) in
            self.originalViewBottomCons = make.bottom.equalTo(pictureView).offset(YJStatusCellMargin).constraint
        }
        
    }
    
    // MARK: - 懒加载
    /// 用户头像
    fileprivate lazy var avatarView = UIImageView(image: defaultAvatar())
    
    /// 用户名
    fileprivate lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: YJStatusCellContentSize)
        return label
    }()
    
    fileprivate lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.orange
        label.font = UIFont.systemFont(ofSize: YJStatusCellBottomDefaultFontSize)
        return label
    }()
    
    fileprivate lazy var sourceLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.font = UIFont.systemFont(ofSize: YJStatusCellBottomDefaultFontSize)
        return label
    }()
    
    /// 会员等级
    fileprivate lazy var memberView: UIImageView = UIImageView()
    
    /// 认证图标
    fileprivate lazy var verifiedView: UIImageView = UIImageView()
    
    /// 原创微博内容
    fileprivate lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: YJStatusCellContentSize)
        label.numberOfLines = 0
        
        // 换行/折行宽度
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * YJStatusCellMargin
        
        return label
    }()
    
    /// 配图视图,不管cell是否要显示图片,先加载进来,如果模型里面不需要显示图片,就把这个配图视图隐藏掉,如果要显示图片就把这个配图视图显示出来
    fileprivate lazy var pictureView: YJStatusPictureView = YJStatusPictureView()

}
